//
//  ShowPictureView.m
//  MiBaoTu
//
//  Created by admin on 16/10/14.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "ShowPictureView.h"
#import "UIImageView+WebCache.h"

@interface ShowPictureView ()<UIScrollViewDelegate> {
    UIScrollView                *_scrollView;
    UILabel                     *_showTipsLb;
    UIWindow                    *_selectWindow;
    NSInteger                  _mark;
}

@end
@implementation ShowPictureView

- (id)initWithDataArr:(NSArray *)picArr withMark:(NSInteger)mark
{
    self = [super init];
    if (self) {
        _mark=mark;
        self.picArr = picArr;
        [self onCreateUIwithMark : mark];
    }
    return self;
}

//- (id)initWithFrame:(CGRect)frame
//{
//    self = [super initWithFrame:CGRectZero];
//    if (self) {
//        [self onCreateUI];
//    }
//    return self;
//}

- (void)setPicArr:(NSArray *)picArr
{
    if (!_picArr) {
        _picArr = [[NSArray alloc]init];
    }
    _picArr = picArr;
    
    if (_scrollView && _scrollView.subviews.count < _picArr.count) {
        [self addPictures:_picArr withMark:_mark];
    }
}

#pragma mark - UI
- (void)onCreateUIwithMark:(NSInteger)mark
{
    self.backgroundColor = [UIColorFromHex(0x000000)colorWithAlphaComponent:.8];
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTapAction)];
    [self addGestureRecognizer:tap];
    
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _scrollView.frame = self.bounds;
    if (ISIPAD) {
        _scrollView.frame=CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    }
    
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    [self addSubview:_scrollView];
    
    UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-100*PER_HEIGHT, SCREEN_WIDTH, 100*PER_HEIGHT)];
    view.backgroundColor=[UIColorFromHex(0x000000) colorWithAlphaComponent:.4];
    [self addSubview:view];
    
    
    _showTipsLb = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-100-25*PERCEN, SCREEN_HEIGHT-30, 100, 20)];
    _showTipsLb.textAlignment = NSTextAlignmentRight;
    _showTipsLb.textColor = [UIColor whiteColor];
    _showTipsLb.font = [UIFont systemFontOfSize:13.];
    [view addSubview:_showTipsLb];
    
    if (_picArr.count > 0) {
        if (ISIPAD) {
            _scrollView.contentSize = CGSizeMake(_scrollView.width*_picArr.count, 0);
        } else {
            _scrollView.contentSize = CGSizeMake(_scrollView.width*_picArr.count, _scrollView.height);
        }
        _showTipsLb.text = [NSString stringWithFormat:@"%ld/%d",mark,(int)_picArr.count];
        [self addPictures:_picArr withMark:mark];
    }
}

- (void)addPictures:(NSArray *)arr withMark:(NSInteger)mark
{
    for (int i=0; i<arr.count; i++) {
        UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(_scrollView.width*i, 0, _scrollView.width, _scrollView.height)];
        imgView.image=[UIImage imageNamed:arr[i]];
//        [imgView sd_setImageWithURL:[NSURL URLWithString:[arr objectAtIndex:i]] placeholderImage:[UIImage imageNamed:@"jiatu"]options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//           
//            CGImageRef imageRef = image.CGImage;
//            float itemChar = MIN(imgView.width/image.size.width, imgView.height/image.size.height);
//            float item_width = itemChar*image.size.width*1.;
//            float item_height = itemChar*image.size.height*1.;
//            
//            CGRect rect;
//            if ((imgView.width-item_width) <= (imgView.height-item_height)) {
//                //宽
//                float top = 0;
//                if (imgView.height >= (imgView.width*item_height/item_width) ) {
//                    top = (imgView.height - (imgView.width*item_height/item_width) )/2.;
//                } else {
//                    top = (-imgView.height + (imgView.width*item_height/item_width) )/2.;
//                }
//                rect = CGRectMake(0, top, imgView.width, imgView.width*item_height/item_width);
//            } else {
//                //高
//                float left = 0;
//                if (imgView.width >= (imgView.height*item_width/item_height) ) {
//                    left = (imgView.width - (imgView.height*item_width/item_height) )/2.;
//                } else {
//                    left = (-imgView.width + (imgView.height*item_width/item_height) )/2.;
//                }
//                rect = CGRectMake(left, 0, imgView.height*item_width/item_height, imgView.height);
//            }
//            
////            imgView.frame = rect;
//            CGImageRef imagePartRef = CGImageCreateWithImageInRect(imageRef, CGRectMake(0, 0, image.size.width, image.size.height));
//            UIImage *rectImg = [UIImage imageWithCGImage:imagePartRef];
//            CGImageRelease(imagePartRef);
//            imgView.image=rectImg;
//
//        }];
//
        imgView.tag = 800+i;
    
       // imgView.contentMode = UIViewContentModeScaleAspectFit;
        [_scrollView addSubview:imgView];
        
    }
    [_scrollView setContentOffset:CGPointMake(mark*SCREEN_WIDTH,0) animated:YES];
}

- (void)onTapAction
{
    [self removeFromWindow];
}

- (void)onShowInWindow
{
    if (!_selectWindow) {
        _selectWindow = [[UIWindow alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.frame];
        _selectWindow.backgroundColor = [UIColorFromHex(0x000000) colorWithAlphaComponent:1];
        [self onTurnToOriention:_orientation];
        [_selectWindow makeKeyAndVisible];
    }
    [_selectWindow addSubview:self];
}

- (void)onTurnToOriention:(UIDeviceOrientation)orientation
{
    if (ISIPAD) {
        _selectWindow.transform = CGAffineTransformMakeRotation(M_PI*.5);
        _selectWindow.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH);
        self.frame = CGRectMake(0, 0, _selectWindow.width, _selectWindow.height);
        _scrollView.frame = CGRectMake(0,SCREEN_WIDTH- (SCREEN_HEIGHT-1200*PERCEN)/2.-1200*PERCEN, SCREEN_WIDTH, 1200*PERCEN);
        for (int i=0;i<_picArr.count;i++) {
            UIImageView *imgView = (UIImageView *)[_scrollView viewWithTag:800+i];
            imgView.frame = CGRectMake(_scrollView.width*i, 0, _scrollView.width, _scrollView.height);
        }
        _showTipsLb.frame = CGRectMake(self.width-100-25*PERCEN, self.width-30, 100, 20);
    } else {
        //                _selectWindow.transform = CGAffineTransformMakeRotation(0);
        //                _selectWindow.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        //                if (ISIPAD) {
        //                    self.frame = CGRectMake((SCREEN_WIDTH-600*PERCEN)/2., (SCREEN_HEIGHT-450*PERCEN)/2., 600*PERCEN, 450*PERCEN);
        //                } else {
        //                    self.frame = CGRectMake((SCREEN_WIDTH-400*PERCEN)/2., (SCREEN_HEIGHT-300*PERCEN)/2., 400*PERCEN, 300*PERCEN);
        //                }
    }
}

- (void)removeFromWindow
{
    [self removeFromSuperview];
    NSMutableArray *windows = [[NSMutableArray alloc]initWithArray:[UIApplication sharedApplication].windows];
    if ([windows containsObject:_selectWindow]) {
        [windows removeObject:_selectWindow];
        _selectWindow = nil;
    }
    
    [windows enumerateObjectsUsingBlock:^(UIWindow  *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.windowLevel == UIWindowLevelNormal && [obj isKindOfClass:[UIWindow class]] && obj.isHidden == NO) {
            [obj makeKeyAndVisible];
            *stop = YES;
        }
    }];
}

#pragma mark - 滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int index = 1+ (int)scrollView.contentOffset.x / scrollView.width;
    _showTipsLb.text = [NSString stringWithFormat:@"%d/%d",index,(int)_picArr.count];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
   int index = 1+ (int)scrollView.contentOffset.x / scrollView.width;
    _showTipsLb.text = [NSString stringWithFormat:@"%d/%d",index,(int)_picArr.count];
}

@end
